<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #div1 {
            width: 100px;
            height: 200px;
            background: pink;
            position: absolute;
            left: -100px;
            top: 200px;
        }

        #div1 span {
            width: 20px;
            height: 60px;
            background: orange;
            position: absolute;
            left: 100px;
            top: 70px;
            line-height: 20px;
        }
    </style>
</head>

<body>
    <div id="div1">
        <span>分享到</span>
    </div>
</body>
<script>
    // 获取元素
    var odiv1 = document.getElementById("div1");
    // var ospan = document.querySelector("#div1 span")
    odiv1.onmouseover = function () {
        // odiv1.style.left=0;
        starMove(5,0);
    }
    odiv1.onmouseout = function () {
        // odiv1.style.left=-100+"px"
        starMove(-5,-100);
    }

    
    var timer = null;
    function starMove(speed,Target) {
        // var speed = 5;
        clearInterval(timer)
        timer = setInterval(function () {
            if (odiv1.offsetLeft == Target) {
                clearInterval(timer)
            } else {
                odiv1.style.left = odiv1.offsetLeft + speed + "px"
            }
        }, 30)
    }

    // function endMove() {
    //     var speed = -5;
    //     clearInterval(timer)
    //     timer = setInterval(function () {
    //         if (odiv1.offsetLeft == -100) {
    //             clearInterval(timer)
    //         } else {
    //             odiv1.style.left = odiv1.offsetLeft + speed + "px"
    //         }
    //     }, 30)
    // }

</script>

</html>